package com.ciwong.epaper.common.dialogbottom;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.adapter.CatalogDialogAdapter;
import com.ciwong.epaper.modules.me.bean.CatalogueUnitBean;
import com.ciwong.epaper.util.ViewUtil;
import com.ciwong.epaper.widget.DividerItemDecoration;
import com.ciwong.epaper.widget.catalog.CatalogNode;
import com.ciwong.epaper.widget.catalog.NodeHelp;
import com.ciwong.epaper.widget.catalog.base.BaseNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CatalogBottomDialog extends BaseBottomDialog {
    private RecyclerView rvDialogCatalog;
    private ItemCallBack itemCallBack;
    private CatalogDialogAdapter catalogDialogAdapter;
    private LinkedList<BaseNode> mLinkedList = new LinkedList<>();

    public interface ItemCallBack {
        void onItemCallBack(CatalogueUnitBean catalogueInfo);
    }

    public void setOnItemCallBackListener(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public CatalogBottomDialog(Context context, int style) {
        super(context, style);
    }

    @Override
    public void initView(Context context) {
        initViewClick();
        initRecycle(context);
    }
    public void initViewClick() {
        View mRootView = LayoutInflater.from(this.getContext()).inflate(R.layout.fragment_catalog_dialog, null);
        mRootView.findViewById(R.id.tl_close).setOnClickListener(this);
        rvDialogCatalog = mRootView.findViewById(R.id.rv_dialog_catalog);
        setContentView(mRootView);
    }

    public void initRecycle(Context context) {
        if (catalogDialogAdapter == null)
            catalogDialogAdapter = new CatalogDialogAdapter(context);
        if (rvDialogCatalog == null) return;
        rvDialogCatalog.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvDialogCatalog.setAdapter(catalogDialogAdapter);
        rvDialogCatalog.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL, ViewUtil.dip2px(context, (float) 0.5), R.color.def_line_color));

        catalogDialogAdapter.setOnItemClickListener(new CatalogDialogAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, List<BaseNode> list, int position) {
                dismiss();
                CatalogNode baseNode = (CatalogNode) list.get(position);
                if (itemCallBack != null)
                    itemCallBack.onItemCallBack(baseNode.getNodeContent());
            }
        });
        catalogDialogAdapter.setData(mLinkedList);

    }

    /**
     * 更新数据
     *
     * @param catalogueUnitBean
     * @param pid
     */
    public void initData(List<CatalogueUnitBean> catalogueUnitBean, String pid) {
        if (catalogueUnitBean == null || catalogueUnitBean.size() <= 0) return;
        List<BaseNode> baseNodes = constructData(catalogueUnitBean, pid);
        mLinkedList.clear();
        mLinkedList.addAll(NodeHelp.sortNode(baseNodes, true));//整理关系
        if (catalogDialogAdapter != null)
            catalogDialogAdapter.setData(mLinkedList);
    }

    private List<BaseNode> constructData(List<CatalogueUnitBean> catalogueUnitBean, String pId) {
        ArrayList<BaseNode> list = new ArrayList();
        if (pId == null) pId = "-1";
        return handleData(catalogueUnitBean, list, pId);
    }

    /**
     * 构造数据
     *
     * @param catalogueUnitBeans
     * @param list
     * @param pId
     * @return
     */
    private List<BaseNode> handleData(List<CatalogueUnitBean> catalogueUnitBeans, List<BaseNode> list, String pId) {
        CatalogNode catalogNode = null;
        for (int i = 0; i < catalogueUnitBeans.size(); i++) {
            CatalogueUnitBean catalogueUnitBean = catalogueUnitBeans.get(i);
            String idself = catalogueUnitBean.getUnitid() == null ? 0 + "" : catalogueUnitBean.getUnitid();
            catalogNode = new CatalogNode(pId, idself, catalogueUnitBean);
            list.add(catalogNode);
            if (catalogueUnitBean.getChildren() != null && catalogueUnitBean.getChildren().size() > 0) {
                String idself_ = catalogueUnitBean.getUnitid() == null ? 0 + "" : catalogueUnitBean.getUnitid();
                handleData(catalogueUnitBean.getChildren(), list, idself_);
            }
        }
        return list;
    }

}
